package com.netease.controller;

import com.alibaba.fastjson2.JSONObject;
import com.netease.config.FlyBookConfig;
import com.netease.config.WeatherConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @projectName: modeling-dev
 * @package: com.netease.controller
 * @className: WeatherController
 * @author: aochong
 * @description: 天气查询controller
 * @date: 2023/4/26 14:57
 * @version: 1.0
 */
@Api(value = "天气查询相关接口", tags = {"天气查询相关接口"})
@RestController
@RequestMapping("weather")
public class WeatherController {

    private final Logger logger = LoggerFactory.getLogger(WeatherController.class);

    @Autowired
    private WeatherConfig weatherConfig;
    @Autowired
    private FlyBookConfig flyBookConfig;

    @Autowired
    RestTemplate restTemplate;

    @ApiOperation(value = "天气查询接口", notes = "请输入要查询的城市编码，默认101210101", httpMethod = "GET")
    @GetMapping("/get/{cityCode}")
    public String weather(@ApiParam(name = "cityCode", value = "城市编码",defaultValue = "101210101", required = false)@PathVariable String cityCode){

        String url = weatherConfig.getUrl() + "?cityIds=" + cityCode;

        ResponseEntity<String> weatherResult = restTemplate.getForEntity(url, String.class);
        JSONObject jsonObject = JSONObject.parseObject(weatherResult.getBody());

        JSONObject requestJson = new JSONObject();
        requestJson.put("msg_type","text");
        requestJson.put("content",jsonObject.getString("code"));

        if (200 == weatherResult.getStatusCodeValue()){
            return jsonObject.toString();
        }else{
            return "error with code: " + weatherResult.getStatusCodeValue();
        }
    }

}
